#include "WString.h"
#ifndef WIFI_H
#define WIFI_H
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>

ESP8266WebServer server(80);

const char *ssid = "zhouliangan44";
const char *password = "wxg13948532823";

Mode stringToMode(const String &str)
{
  if (str == "Skyarrow_Single")
    return Skyarrow_Single;
  if (str == "Skyarrow_Color")
    return Skyarrow_Color;
  if (str == "Bloom")
    return Bloom;
  if (str == "Launch_Bg")
    return Launch_Bg;
  if (str == "Launch_Cs")
    return Launch_Cs;
  if (str == "Launch_As")
    return Launch_As;
  if (str == "Rainbowgradient")
    return Rainbowgradient;
  if (str == "Fivepointflow")
    return Fivepointflow;
  if (str == "Breath")
    return Breath;
  return InvalidMode; // 用于处理无效的字符串
}

Control_type stringToControlType(const String &str)
{
  if (str == "SINGLE")
    return SINGLE;
  if (str == "CIRCULATION")
    return CIRCULATION;
  if (str == "RANDOM")
    return RANDOM;
  return INVALID; // 用于处理无效的字符串
}

class Web
{
public:
  static void handleSearch()
  {
    String val1 = server.arg("bright");
    String val2 = server.arg("delay_time");
    String val3 = server.arg("control_type");
    String val4 = server.arg("mode");
    String val5 = server.arg("hue");
    String val6 = server.arg("hue_bg");
    if (String("") != val6)
    {
      hue[1] = val6.toInt();
      Serial.print("hue_bg=");
      Serial.println(hue[1]);
    }

    if (String("") != val5)
    {
      // display to preview
      fill_solid(leds, NUM_LEDS, CHSV(val5.toInt(), 255, 255));
      FastLED.show();
      switch (mode)
      {
      case Skyarrow_Single:
        hue[0] = val5.toInt();
        Serial.print("hue=");
        Serial.println(hue[0]);
        break;
      case Bloom:
        hue[2] = val5.toInt();
        Serial.print("hue=");
        Serial.println(hue[2]);
        break;
      case Launch_Cs:
        hue[4] = val5.toInt();
        Serial.print("hue=");
        Serial.println(hue[4]);
        break;
      case Launch_As:
        hue[5] = val5.toInt();
        Serial.print("hue=");
        Serial.println(hue[5]);
        break;
      case Launch_Bg:
        hue[3] = val5.toInt();
        Serial.print("hue=");
        Serial.println(hue[3]);
        break;
      }
    }

    if (String("") != val4)
    {
      isMODEModified = true;
      mode = stringToMode(val4);
      Serial.println("mode=" + val4 + mode);
      Serial.println("cismodemodified=" + isMODEModified);
    }
    if (String("") != val3)
    {
      control_type = stringToControlType(val3);
      Serial.println("control_type=" + val3 + control_type);
    }
    if (String("") != val1)
    {
      max_bright = val1.toInt();
      bright = max_bright;
      FastLED.setBrightness(bright);
      Serial.printf("\nbright=%d\n", bright);
    }
    if (String("") != val2)
    {
      Delay_Time = val2.toInt();
      Serial.printf("\nDelay_time=%d\n", Delay_Time);
    }
  }

  static void handleJS()
  {
    String path = "/indexjs.js";
    File file = SPIFFS.open(path, "r");
    server.streamFile(file, "text/javascript");
  }
  static void handleRoot() // Declare as a static member function
  {
    String path = "/index.html";
    File file = SPIFFS.open(path, "r");
    server.streamFile(file, "text/html");
  }

  void connect(const char *ssid, const char *password)
  {
    Serial.begin(115200);
    delay(10);
    Serial.println('\n');

    WiFi.begin(ssid, password);

    Serial.print("Connecting to:");
    Serial.println(ssid);
    while (WiFi.status() != WL_CONNECTED)
    {
      delay(500);
      Serial.print(".");
    }

    Serial.printf("\n WiFi connected!\n IP address:");
    Serial.println(WiFi.localIP());
  }

  void beginserver()
  {
    server.on("/", handleRoot);         // set handele function
    server.on("/indexjs.js", handleJS); // set handele function
    server.on("/search", handleSearch);

    server.begin();
    Serial.println("HTTP server started");
  }

  void handleRequest()
  {
    server.handleClient();
  }
};

// Instantiate the Web class object
Web Webctrl;
#endif // WIFI_H
